package gp;

import java.util.ArrayList;
import java.util.List;

import firing.FiringTree;


public class Generationator {
    
    public static List<FiringTree> getNextGen(List<FiringTree> prevGen, IndexSelectionStrategy strategy) {
	ArrayList<FiringTree> result = new ArrayList<FiringTree>();
	for(int i=0;i<prevGen.size();i++) {
	    int index = strategy.getIndex(prevGen.size()); 
	    
	    if (Math.random() < .5 || i == prevGen.size()-1) { //prevent final mutation from adding 2 trees and increasing population size
		FiringTree parent = prevGen.get(index);
		FiringTree child = parent.duplicate();
		Mutator.gMutate(child);
		result.add(child);
	    } else {
		
		int index2 = strategy.getIndex(prevGen.size()); 
		FiringTree child1 = prevGen.get(index).duplicate();
		FiringTree child2 = prevGen.get(index2).duplicate();
		Mutator.gCrossover(child1, child2);
		
		result.add(child1);
		result.add(child2);
		i++;
	    }
	}
	return result;
    }
}
